﻿ 
 
#ifdef __D3DX9LOADER_TEST_MESH_COLLISION__
 
//#include "stdafx.h"

#include <d3dx9-dynamic-load/d3dx9_dynamic_load.h>
using d3dx9_dynamic_load::CD3DX9_Dll_Loader;

#include <d3dx9-dynamic-load/mesh.h>
using d3dx9_dynamic_load::Mesh;
 
#include <dxut_wonly/dxut_wonly.h>


static CD3DX9_Dll_Loader*  g_loader = NULL;
static Mesh *  g_mesh = NULL;
static  D3DXMATRIX   g_mView; 
static  D3DXMATRIX   g_mProj;


//--------------------------------------------------------------------------------------
// Rejects any devices that aren't acceptable by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
								 D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
{
	// Typically want to skip backbuffer formats that don't support alpha blending
	IDirect3D9* pD3D = DXUTGetD3DObject(); 
	if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
		AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, 
		D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
		return false;

	return true;
}


//--------------------------------------------------------------------------------------
// Before a device is created, modify the device settings as needed
//--------------------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
{
	return true;
}


//--------------------------------------------------------------------------------------
// Create any D3DPOOL_MANAGED resources here 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
	HRESULT hr=0;
 
		 g_loader = new CD3DX9_Dll_Loader();

 


	return  hr;
}


//--------------------------------------------------------------------------------------
// Create any D3DPOOL_DEFAULT resources here 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
							   const D3DSURFACE_DESC* bbsd, void* pUserContext )
{



	g_loader->D3DXMatrixPerspectiveFovLH(&g_mProj, 
	 D3DX_PI * 0.25f,  (FLOAT)bbsd->Width/(FLOAT)bbsd->Height, 0.1f, 1000.0f )   ;


	return S_OK;
}


//--------------------------------------------------------------------------------------
// Handle updates to the scene
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( IDirect3DDevice9* device, double fTime, float fElapsedTime, void* pUserContext )
{


}


//--------------------------------------------------------------------------------------
// Render the scene 
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameRender( IDirect3DDevice9* device, double fTime, float fElapsedTime, void* pUserContext )
{
	HRESULT hr =0;

	// Clear the render target and the zbuffer 
	V( device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0) );

	// Render the scene
	if( SUCCEEDED( device->BeginScene() ) )
	{
	  
  g_loader->D3DXMatrixLookAtLH(&g_mView, 
		   &D3DXVECTOR3(5,0,0), &D3DXVECTOR3(0,0,0), &D3DXVECTOR3(0,1,0) );

 
		     
  hr |= device->SetTransform(D3DTS_VIEW, &g_mView );
  hr |= device->SetTransform(D3DTS_PROJECTION, &g_mProj );

 
  if(!g_mesh)
  {
	   d3dx9_dynamic_load::meshCreateOptions opt;
	   g_mesh = new Mesh(device, g_loader,
		   L"j:/d3dx9_dynamic_load/d3dx9-dynamic-load/media/mesh.x",
		   opt	);
  }

 


 
	  if( g_mesh )
	  {
			hr |= g_mesh->DrawSubset(0);
	  }
 



		V( device->EndScene() );
	}


	assert(hr==0);
}


//--------------------------------------------------------------------------------------
// Handle messages to the application 
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, 
						 bool* pbNoFurtherProcessing, void* pUserContext )
{




	return 0;
}


//--------------------------------------------------------------------------------------
// Release resources created in the OnResetDevice callback here 
//--------------------------------------------------------------------------------------
void CALLBACK OnLostDevice( void* pUserContext )
{
	if(g_mesh)
	{
		delete g_mesh;
		g_mesh = NULL;
	}


}


//--------------------------------------------------------------------------------------
// Release resources created in the OnCreateDevice callback here
//--------------------------------------------------------------------------------------
void CALLBACK OnDestroyDevice( void* pUserContext )
{

	if(g_mesh)
	{
		delete g_mesh;
		g_mesh = NULL;
	}


	if(g_loader)
	{
		delete g_loader;
		g_loader = NULL;
	}
}



//--------------------------------------------------------------------------------------
// Initialize everything and go into a render loop
//--------------------------------------------------------------------------------------
//INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
int main(int argc,  char* argv[])
{
	// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

	// Set the callback functions
	DXUTSetCallbackDeviceCreated( OnCreateDevice );
	DXUTSetCallbackDeviceReset( OnResetDevice );
	DXUTSetCallbackDeviceLost( OnLostDevice );
	DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
	DXUTSetCallbackMsgProc( MsgProc );
	DXUTSetCallbackFrameRender( OnFrameRender );
	DXUTSetCallbackFrameMove( OnFrameMove );

	// TODO: Perform any application-level initialization here

	// Initialize DXUT and create the desired Win32 window and Direct3D device for the application
	DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
	DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
	DXUTCreateWindow( L"EmptyProject" );
	DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );

	// Start the render loop
	DXUTMainLoop();

	// TODO: Perform any application-level cleanup here

	return DXUTGetExitCode();
}




#endif // __D3DX9LOADER_TEST_MESH_COLLISION__
